package cn.tradewin.advisor.pages.passets.master;

import java.util.List;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.persist.model.ChkCategory;
import cn.tradewin.advisor.persist.services.CodingChkService;
import cn.tradewin.advisor.persist.services.PassetsMasterService;

public class ChkCategoryMaster {

	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<ChkCategory> beanModel;
	
	@Inject
	private Messages messages;
	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private Integer lang;
	
	@Property
	private SelectModel langModel;
	
	@Inject
	private CodingChkService codeChkService;
	
	@Inject
	private PassetsMasterService passetsMasterService;
	
	@Property
	private String content;
	
	@Property
	private List<ChkCategory> categories;
	
	@Property
	private ChkCategory category;
	
	@InjectComponent
	private Zone recordsZone;
	
	@Inject
	private Request request;
	
	@Inject
    private AjaxResponseRenderer ajaxResponseRenderer;
	
	void init() {
		beanModel = beanModelSource.createDisplayModel(ChkCategory.class, messages);
		beanModel.add("action", null);
		beanModel.include("langName","desc", "action");
		beanModel.get("langName").sortable(false);
		beanModel.get("desc").sortable(false);
	}
	
	void setupRender() {
		init();
		langModel = new CodeLabelSelectModel(codeChkService.getLangOptions());
		categories = passetsMasterService.getChkCategories(lang);
	}
	
	void onSubmit() {
		ChkCategory in = new ChkCategory();
		in.setLang(lang);
		in.setDesc(content);
		passetsMasterService.addChkCategory(in);
	}
	
	void onValueChangedFromLang(Integer lang) {
		 if (request.isXHR()) {
			categories = passetsMasterService.getChkCategories(lang);
			init();
			ajaxResponseRenderer.addRender(recordsZone);
	    }
	}
	
	void onDelete(Integer code) {
		passetsMasterService.delChkCategory(code);
	}
}
